package com.lgb.Service;

import com.lgb.DAO.BlogRepository;
import com.lgb.Exceptions.NotFoundException;
import com.lgb.pojo.Blog;
import com.lgb.pojo.Type;
import com.lgb.utils.MarkDownUtils;
import com.lgb.utils.MyBeansUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import com.lgb.vo.BlogQuery;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BlogServiceImpl implements BlogService{

    @Autowired
    private BlogRepository blogRepository;

    @Override
    public Blog getBlog(Long id) {
        return blogRepository.getOne(id);
    }

    @Override
    public Blog getAndConvert(Long id) {
        Blog blog = blogRepository.getOne(id);
        if(blog == null){
            throw new NotFoundException("博客不存在");
        }
        //blog的view_times + 1
        blog.setView_times(blog.getView_times() + 1);
        //保存
        blogRepository.save(blog);
        //创建新的blog对象
        Blog b = new Blog();
        //把blog的属性值给b
        BeanUtils.copyProperties(blog,b);
        String content = b.getContent();
        //处理内容
        String new_content = MarkDownUtils.markdownToHtmlExtensions(content);
        //把处理好的内容赋值给blog
        b.setContent(new_content);
        return blogRepository.save(b);
    }

    @Override
    public Blog saveBlog(Blog blog) {
        //新增
        if(blog.getId() == null){
            //初始化时间
            blog.setCreateTime(new Date());
            blog.setUpdateTime(new Date());
            //初始化浏览次数
            blog.setView_times(0);
        }else {
            //修改更新时间
            blog.setUpdateTime(new Date());
        }

        return blogRepository.save(blog);
    }

    @Override
    public Blog updateBlog(Long id, Blog blog) {
        Blog b = blogRepository.getOne(id);
        if(b == null){
            throw new NotFoundException("该博客不存在");
        }
        //把blog的值赋给b
        //如果直接赋值的话，blog中某些为null的值也会给b，所以这里只传递属性值不为空的属性
        BeanUtils.copyProperties(blog,b, MyBeansUtils.getNullPropertyNames(blog));
        b.setUpdateTime(new Date());
        return blogRepository.save(b);
    }

    @Override
    public void deleteBlog(Long id) {
        blogRepository.deleteById(id);
    }

    @Override
    public Page<Blog> listBlog(Pageable pageable, BlogQuery blog) {
        return blogRepository.findAll(new Specification<Blog>() {
            @Override
            public Predicate toPredicate(Root<Blog> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                //条件集合
                List<Predicate> predicates = new ArrayList<>();

                //文章标题模糊查询
                if(!"".equals(blog.getTitle()) && blog.getTitle() != null){
                    predicates.add(criteriaBuilder.like(root.<String>get("title") , "%" +blog.getTitle() + "%"));
                }

                //分类查询
                if (blog.getTypeId() != null){
                    predicates.add(criteriaBuilder.equal(root.<Type>get("type").get("id") , blog.getTypeId()));
                }

                //是否推荐
                if(blog.getRecommend() == true){
                    predicates.add(criteriaBuilder.equal(root.<Boolean>get("recommend") , blog.getRecommend()));
                }

                //进行查询 传递数据为数组
                criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()]));
                return null;
            }
        }, pageable);
    }

    @Override
    public Page<Blog> listBlog(Pageable pageable) {
        return blogRepository.findAll(pageable);
    }

    @Override
    public Page<Blog> listBlog(String query , Pageable pageable) {
        return blogRepository.findByQuery(query,pageable);
    }
}
